import request from '@/utils/request'

export function getList(type, params) {
  let op_id = ''
  switch (type) {
    case 'group':
      op_id = 'getGroup'
      break
    case 'getOrgan':
      op_id = 'getOrgan'
      break
    case 'constant':
      op_id = 'getAllConstant'
      break
    default:
      break
  }
  params = Object.assign({ op_id }, params)
  return new Promise((resolve) => {
    const url = params.op_id
    request({
      url,
      method: 'get',
      params
    })
      .then((res) => {
        resolve(res)
      })
      .catch(() => {
        const data = {
          total: 0,
          pageItems: []
        }
        resolve({ data })
      })
  })
}

export function addOne(type, data) {
  let op_id = ''
  switch (type) {
    case 'insertGroup':
      op_id = 'insertGroup'
      break
  }
  const params = { op_id }
  // 过滤掉data中无效字段
  const tmp = {}
  for (const key of Object.keys(data)) {
    if (data[key] || data[key] === 0) {
      tmp[key] = data[key]
    }
  }
  return new Promise((resolve) => {
    const url = params.op_id
    request({
      url,
      method: 'post',
      params,
      data: tmp
    })
      .then(res => {
        resolve(res)
      })
      .catch(() => {
        const data = {
          total: 0,
          pageItems: []
        }
        resolve(data)
      })
  })
}

export function updateOne(type, data) {
  let op_id = ''
  switch (type) {
    case 'updateGroup':
      op_id = 'updateGroup'
      break
    case 'organization':
      op_id = 'updateOrgan'
      break
  }
  const params = { op_id }
  // 过滤掉data中无效字段
  const tmp = {}
  for (const key of Object.keys(data)) {
    if (data[key] || data[key] === 0) {
      tmp[key] = data[key]
    }
  }
  return new Promise((resolve) => {
    const url = params.op_id
    request({
      url,
      method: 'post',
      params,
      data: tmp
    })
      .then(res => {
        resolve(res)
      })
      .catch(() => {
        const data = {
          total: 0,
          pageItems: []
        }
        resolve(data)
      })
  })
}

export function addOneByForm(type, data) {
  let op_id = ''
  switch (type) {
    case 'organization':
      op_id = 'insertOrgan'
      break
  }
  const params = { op_id }
  // 过滤掉data中无效字段
  const tmp = {}
  for (const key of Object.keys(data)) {
    if (data[key] || data[key] === 0) {
      tmp[key] = data[key]
    }
  }
  return new Promise((resolve) => {
    const url = params.op_id
    request({
      url,
      method: 'post',
      params,
      data: tmp
    })
      .then(res => {
        resolve(res)
      })
      .catch(() => {
        const data = {
          total: 0,
          pageItems: []
        }
        resolve(data)
      })
  })
}

export function updateOneByForm(type, data) {
  let op_id = ''
  switch (type) {
    case 'organization':
      op_id = 'updateOrgan'
      break
  }
  const params = { op_id }
  // 过滤掉data中无效字段
  const tmp = {}
  for (const key of Object.keys(data)) {
    if (data[key] || data[key] === 0) {
      tmp[key] = data[key]
    }
  }
  return new Promise((resolve) => {
    const url = params.op_id
    request({
      url,
      method: 'post',
      params,
      data: tmp
    })
      .then(res => {
        resolve(res)
      })
      .catch(() => {
        const data = {
          total: 0,
          pageItems: []
        }
        resolve(data)
      })
  })
}

export function deleteItem(type, data) {
  let op_id = ''
  switch (type) {
    case 'organization':
      op_id = 'deleteOrgan'
      break
  }
  const params = { op_id }
  // 过滤掉data中无效字段
  const tmp = {}
  for (const key of Object.keys(data)) {
    if (data[key] || data[key] === 0) {
      tmp[key] = data[key]
    }
  }
  return new Promise((resolve) => {
    const url = params.op_id
    request({
      url,
      method: 'post',
      params,
      data: tmp
    })
      .then(res => {
        resolve(res)
      })
      .catch(() => {
        const data = {
          total: 0,
          pageItems: []
        }
        resolve(data)
      })
  })
}
